$(function () {
    const form = layui.form
    form.verify({
        pwd: [/^\S{6,12}$/, '密码必须为6-12, 并且不能为空格!'],
        nPwd: value => {
            if(value == $('input[name=oldPwd]').val()) return '不能与原密码相同！'
        }, 
        rpPwd: value => {
            if(value !== $('input[name=newPwd]').val()) return '两次密码不一致！'
        }
    })
    $('.layui-form').on('submit' ,function(){
        const data =  form.val('form-pwd')
        $.post('/my/updatepwd', data).then(res => {
            if(res.status !== 0){
                return layer.layer.msg(res.message || '修改密码失败')
            }
            layui.layer.msg('修改密码成功！')
            layui.layer.alert('修改密码成功, 即将跳转到登录...', {
                time: 3 * 1000,
                success: function(layero, index){
                    var timeNum = this.time / 1000, setText = function(start){
                        layer.title((start ? timeNum : --timeNum) + ' 秒后关闭', index);
                    };
                    setText(!0);
                    this.timer = setInterval(setText, 1000);
                    if(timeNum <= 0) clearInterval(this.timer);
                },
                end: function(){
                    // 跳转到登录
                    // 1. 跳转到登录页面 
                    parent.location.href = '/login.html'
                    // 2. 删除token
                    localStorage.removeItem('BIG_EVENT_TOKEN')
                }
            })
        })
        return false        
    })
})